package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbOrderConfigMapper;
import com.guigu.qingcheng.pojo.TbOrderConfig;
import com.guigu.qingcheng.service.TbOrderConfigService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


/**
*
* @author Jiang
* @date 2020/07/31
*/
@Service
public class TbOrderConfigServiceImpl implements TbOrderConfigService {
    @Autowired(required = false)
    private TbOrderConfigMapper tbOrderConfigMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public List<TbOrderConfig> queryAll() {
        List<TbOrderConfig> tbOrderConfig = tbOrderConfigMapper.selectAll();
            return tbOrderConfig;
    }

    @Override
    public PageUtil<TbOrderConfig> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbOrderConfig> pages = (Page<TbOrderConfig>) tbOrderConfigMapper.selectAll();
        PageUtil<TbOrderConfig> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbOrderConfig> pageQueryByAll(Integer pageNum, Integer pageSize, TbOrderConfig tbOrderConfig) {

        Example example=new Example(TbOrderConfig.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        //if(!StringUtils.isEmpty(brand.getId())){
        //    criteria.andEqualTo("id",brand.getId());
        //}
        //if(!StringUtils.isEmpty(brand.getName())){
        //   criteria.andLike("name","%"+brand.getName()+"%");
        //}
        PageHelper.startPage(pageNum,pageSize);
        Page<TbOrderConfig> pages = (Page<TbOrderConfig>) tbOrderConfigMapper.selectByExample(example);
        PageUtil<TbOrderConfig> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbOrderConfig(TbOrderConfig tbOrderConfig) {
        int insert = tbOrderConfigMapper.insert(tbOrderConfig);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbOrderConfig(TbOrderConfig tbOrderConfig) {
        int i = tbOrderConfigMapper.updateByPrimaryKeySelective(tbOrderConfig);
        if(i>0){
            amqpTemplate.convertAndSend("queue.ordersj",tbOrderConfig);
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbOrderConfigMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbOrderConfig findById(Integer id) {
        TbOrderConfig tbOrderConfig = tbOrderConfigMapper.selectByPrimaryKey(id);
        return tbOrderConfig;
    }
}
